package lsl.leetCode.year2023.test0126;



public class test283 {
    public static void main(String[] args) {
        int[] nums = {1, 0, 0, 3, 12};
        moveZeroes(nums);
        System.out.println(nums);
    }

    //双指针
    public static void moveZeroes(int[] nums) {
        int n = nums.length;
        //左指针指向当前已经处理好的序列的尾部(左指针左边均为非零数)
        int left = 0;
        //右指针指向待处理序列的头部(右指针左边直到左指针处均为零)
        int right = 0;
        while (right < n) {
            if (nums[right] != 0) {
                //每次交换,都是将左指针的零与右指针的非零数交换,且非零数的相对顺序并未改变。
                swap(nums, left, right);
                left++;
            }
            right++;
        }
    }

    public static void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
}
